<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>{$appname} | 支付接口</title>
    <meta name="description" content="夺宝支持信息显示界面"/>
    <meta name="keywords" content="夺宝"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="/css/style.css"/>
    <script type="text/javascript" src="/js/layermobile/layer.js"></script>
    <script type="text/javascript" src="/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src="/js/jquery-2.1.4.min.js"></script>
</head>
<body>
<div class="wrapper">
    <div class="innner order_div" style="padding-top: 48px;">
        <div class="order" style="min-height: calc(100vh - 244px);">
            <div class="order_detail">
                <div class="order_title">
                    <span><i class="icon icon_bag"></i>订单</span>
                </div>
                <table>

                    <tr class="p_one_row">
                        <td>订单号</td>
                        <td id="order">{$order}</td>
                    </tr>
                    <tr class="p_one_row">
                        <td>总金额</td>
                        <td>￥{$money}</td>
                    </tr>
                </table>
                <input type="hidden" value="{$token}" id="token">
                <input type="hidden" value="{$payurl}" id="payurl">
                <input type="hidden" value="{$front}" id="front">
            </div>
        </div>
        <div class="foot_div">
            <button id="pay" class="foot_btn">支付 ￥{$money}</button>
            <div class="footnote" style="height: 110px; line-height: 94px;"></div>
        </div>
    </div>
</div>

    <script type="text/javascript">
        $("#pay").attr({ disabled: false });
        var payurl = $('#payurl').val();
        var fronturl = $("#front").val();
        $(function () {
            $("#pay").click(function () {
                var order = $('#order').text();
                var token = $('#token').val();
                var url = payurl+'/Pay/Pay?token='+token;

                $.ajax({
                    type:"post",
                    data:{order:order},
                    url:url,
                    beforeSend:function(){
                        // 禁用按钮防止重复提交
                        $("#pay").attr({ disabled: "disabled" });
                        layer.open({type: 2});
                    },
                    success:function(data){
                        $("#pay").attr({ disabled: false });
                        layer.closeAll();
                        if(data.type == "balances"){
                            if(data.status == true){
                                layer.open({
                                    content: '支付成功',
                                    btn: ['OK'],
                                    yes: function (index) {
                                        window.location.href = fronturl+"/?c=user&res=clear";
                                        layer.close(index);
                                    }
                                });

//                                setTimeout(function(){
//                                    window.location.href = fronturl+"/?c=user&res=clear";
//                                },3000);
                            }else{
                                layer.open({
                                    content: '支付失败',
                                    btn: ['OK'],
                                    yes: function (index) {
                                        window.location.href = fronturl+"/?c=user&res=clear";
                                        layer.close(index);
                                    }
                                });
//                                setTimeout(function(){
//                                    window.location.href = fronturl+"/?c=user&res=clear";
//                                },3000);
                            }
                        }else{
                            data = JSON.parse(data.order);
                            //alert(data.appId);
                            callpay(data);
                        }
                    }
                });

//                $.post(url,{order:order},function(data){
//                    if(data.type == "balances"){
//                        if(data.status == true){
//                            layer.open({
//                                content: '支付成功',
//                                btn: ['OK']
//                            });
//                            setTimeout(function(){
//                                window.location.href = fronturl+"/?c=user&res=clear";
//                            },3000);
//                        }else{
//                            layer.open({
//                                content: '支付失败',
//                                btn: ['OK']
//                            });
//                            setTimeout(function(){
//                                window.location.href = fronturl+"/?c=user&res=clear";
//                            },3000);
//                        }
//                    }else if(data.type== 'weixin'){
//                        data = JSON.parse(data.order);
//                        //alert(data.appId);
//                        callpay(data);
//                    }
//                },'json');
            });
        });

        //调用微信JS api 支付
        function jsApiCall(order)
        {
            WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',
                    order
                    ,function(res){
                        WeixinJSBridge.log(res.err_msg);
                        if(res.err_msg == "get_brand_wcpay_request:ok"){
                            //alert(res.err_code+res.err_desc+res.err_msg);
                            layer.open({
                                content: '支付成功',
                                btn: ['OK'],
                                yes: function (index) {
                                    window.location.href = fronturl+"/?c=user&res=clear";
                                    layer.close(index);
                                }
                            });
//                            setTimeout(function(){
//                                window.location.href = fronturl+"/?c=user&res=clear";
//                            },3000);
                        }else{
                            //返回跳转到订单详情页面
                            layer.open({
                                content: '支付失败',
                                btn: ['OK'],
                                yes: function (index) {
                                    window.location.href = fronturl+"/?c=user&res=clear";
                                    layer.close(index);
                                }
                            });
//                            window.location.href=fronturl + "/?c=user&res=clear";

                        }
                    }
            );
        }

        function callpay(order)
        {

            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                jsApiCall(order);
            }
        }
    </script>

</body>
</html>